package com.outsource.framework.controller;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.outsource.framework.domain.Thaoduan;
import com.outsource.framework.model.services.ThaoduanService;
import com.outsource.framework.util.common.Response;

@Controller
public class HaoduanContrller {

	@Autowired
	ThaoduanService service;
	
	@Autowired
	MessageSource messageSource;
	
	/**
	 * get Attribution
	 * @param usermobile
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/WoWebAttribution",method = {RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody Map<String, Object> WoWebAttribution(
			@RequestParam("usermobile") String usermobile,
			HttpServletRequest request){
		String responseCode = getMessages("lable.code.success");
		String responseMessages = getMessages("lable.success");
		Thaoduan responseEntry = service.findByAttribution(usermobile);
		if(responseEntry!=null){
			String pro = service.findLocation(responseEntry.getProvince());
			String city = service.findLocation(responseEntry.getCity());
			responseEntry.setProvince(pro);
			responseEntry.setCity(city);
		}else{
			responseCode = getMessages("lable.code.error.notfind");
			responseMessages = getMessages("lable.error.pc");
		}
		
		Response response = new Response();
		Map<String, Object> responsemap = response.getResponse(responseCode, responseMessages, responseEntry);
		return responsemap;
	}
	
	/**
	 * get zh.properties content
	 * @param code
	 * @return
	 */
	public String getMessages(String code){
		return messageSource.getMessage(code, null,Locale.SIMPLIFIED_CHINESE);
	}
}
